package com.joolun.web.controller.promotion;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.joolun.system.domain.EduCourse;
import com.joolun.system.domain.vo.EduCourseVo;
import com.joolun.system.service.IEduCourseService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 招生课程Controller
 * 
 * @author joolun
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/promotion/course")
public class EduCourseController extends BaseController
{
    @Autowired
    private IEduCourseService eduCourseService;

    /**
     * 查询招生课程列表
     */
    @GetMapping("/list")
    public TableDataInfo list(EduCourse eduCourse)
    {
        startPage();
        List<EduCourse> list = eduCourseService.selectEduCourseList(eduCourse);
        return getDataTable(list);
    }
    /**
     * 根据排期id查询课程信息
     */
    @GetMapping("/bySchedulingId")
    public AjaxResult getCourseBySchedulingId(Long schedulingId)
    {
        EduCourse course = eduCourseService.getCourseBySchedulingId(schedulingId);
        return success(course);
    }
    /**
     * 导出招生课程列表
     */
    @PreAuthorize("@ss.hasPermi('promotion:course:export') || @ss.hasPermi('advanced:course:export')")
    @Log(title = "招生课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EduCourse eduCourse)
    {
        List<EduCourse> list = eduCourseService.selectEduCourseList(eduCourse);
        ExcelUtil<EduCourse> util = new ExcelUtil<>(EduCourse.class);
        util.exportExcel(response, list, "招生课程数据");
    }

    /**
     * 获取招生课程详细信息
     */
    @GetMapping(value = "/{courseId}")
    public AjaxResult getInfo(@PathVariable("courseId") Long courseId)
    {
        return success(eduCourseService.selectEduCourseByCourseId(courseId));
    }

    /**
     * 新增招生课程
     */
    @PreAuthorize("@ss.hasPermi('promotion:course:add') || @ss.hasPermi('advanced:course:add')")
    @Log(title = "招生课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EduCourse eduCourse)
    {
        eduCourse.setCreateBy(getUsername());
        return toAjax(eduCourseService.insertEduCourse(eduCourse));
    }

    /**
     * 修改招生课程
     */
    @PreAuthorize("@ss.hasPermi('promotion:course:edit') || @ss.hasPermi('advanced:course:edit')")
    @Log(title = "招生课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EduCourse eduCourse)
    {
        eduCourse.setCreateBy(getUsername());
        return toAjax(eduCourseService.updateEduCourse(eduCourse));
    }

    /**
     * 删除招生课程
     */
    @PreAuthorize("@ss.hasPermi('promotion:course:remove') || @ss.hasPermi('advanced:course:remove')")
    @Log(title = "招生课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{courseIds}")
    public AjaxResult remove(@PathVariable Long[] courseIds)
    {
        return toAjax(eduCourseService.deleteEduCourseByCourseIds(courseIds));
    }

    /**
     * 根据id修改排序
     */
    @Log(title = "招生课程", businessType = BusinessType.UPDATE)
    @GetMapping("/updateSort")
    public AjaxResult updateSort(Integer id,Integer courseSort)
    {
        LambdaUpdateWrapper<EduCourse> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(EduCourse::getCourseSort, courseSort);
        wrapper.eq(EduCourse::getCourseId, id);
        return toAjax(eduCourseService.update(wrapper));
    }

    /**
     * 课程下拉列表渲染
     */
    @GetMapping("/list/type")
    public AjaxResult getCourseTypeList(String course_type1,String course_type2)
    {
        List<EduCourseVo> list = eduCourseService.selectCourseAndTeacherList(course_type1,course_type2);
        return success(list);
    }
}
